//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

package uxgenes.view;

// #ifdef WALLPAPER

import java.util.ArrayList;

import uxgenes.app.MainWallpaperService;
import uxgenes.core.Logger;
import uxgenes.core.UXUtils;

import android.annotation.SuppressLint;
import android.app.WallpaperManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.service.wallpaper.WallpaperService;
import android.view.MotionEvent;
import android.view.SurfaceHolder;


//
// Wallpaper Service
//
public class UXWallpaperService extends WallpaperService
								implements uxgenes.core.EngineContext.ActivityInterface
{
// constants //
	private static final String	TAG = "UXWallpaperService";
	
	private static final int	STATE_DESTROYED	= 0;
	private static final int	STATE_STOPED	= 1;
	private static final int	STATE_PAUSED	= 2;
	private static final int	STATE_ACTIVE	= 3;

	
// variables //
	protected 	uxgenes.core.EngineContext	engine;

	private 	ArrayList<WallpaperEngine>	_instances = new ArrayList<WallpaperEngine>();

	private 	int		_state 			= STATE_DESTROYED;
	private		int		_counter 		= 0;
	
	
// methods //
	
	// onCreate
	@Override
	public void onCreate ()
	{
		Logger.I( TAG, "OnCreate" );
		super.onCreate();
		engine = new uxgenes.core.EngineContext( this, this );
		_Create();
		_Start();

		uxgenes.app.WallpaperPreferencesActivity.OnWallpaperCreated( this );
	}
	
	
	// onDestroy
	@Override
	public void onDestroy ()
	{
		Logger.I( TAG, "onDestroy" );
		super.onDestroy();

		_Pause();
		_Stop();
		_Destroy();
	}

	
	// onCreateEngine
	@Override
	public Engine onCreateEngine ()
	{
		return new WallpaperEngine( ++_counter );
	}
	
	
	// SetWallpaper
	@SuppressLint("InlinedApi")
	public final static void SetWallpaper (Context ctx)
	{
		Intent intent = new Intent();
	
		if ( UXUtils.CheckAPIVersion( 16 ) )
		{
			intent.setAction( WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER );
			intent.putExtra( WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
							 new ComponentName( ctx, MainWallpaperService.class ) );
		}
		else {
			intent.setAction( WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER );
		}
		
		ctx.startActivity( intent );
	}
	
	

// EngineContext.ActivityInterface //
	
	// Close
	public final void Close ()
	{
		Logger.I( TAG, "Close" );
		
		stopSelf();
	}

	
	// SetRequestedOrientation
	public final void SetRequestedOrientation (int orient)
	{
		Logger.I( TAG, "can't change wallpaper orientation" );
		// do nothing
	}
	
	
	
// hidden //
	
	// _Create
	protected final void _Create ()
	{
		if ( _state == STATE_DESTROYED ) {
			Logger.I( TAG, "_Create" );
			
			_state = STATE_STOPED;
			engine.InitEngine( false );
			engine.CreateEngineForWallpaper();
		}
	}
	
	
	// _Destroy
	protected final void _Destroy ()
	{
		if ( _state == STATE_STOPED ) {
			Logger.I( TAG, "_Destroy" );
			
			_state = STATE_DESTROYED;
			engine.OnDestroy();
		}
	}
	
	
	// _Start
	protected final void _Start ()
	{
		if ( _state == STATE_STOPED ) {
			Logger.I( TAG, "_Start" );
			
			_state = STATE_PAUSED;
			engine.OnStart();
		}
	}
	
	
	// _Resume
	protected final void _Resume ()
	{
		if ( _state == STATE_PAUSED ) {
			Logger.I( TAG, "_Resume" );
			
			_state = STATE_ACTIVE;
			engine.OnResume();
		}
	}
	
	
	// _ResumeOnSurfaceCreated
	protected final void _ResumeOnSurfaceCreated (SurfaceHolder holder)
	{
		_Resume();
		engine.surfaceCreated( holder );
	}
	
	
	// _ResumeOnSurfaceChanged
	protected final void _ResumeOnSurfaceChanged (SurfaceHolder holder, int format, int width, int height)
	{
		_Resume();
		
		if ( holder != null )
			engine.surfaceChanged( holder, format, width, height );
	}
	
	
	// _OnVisibilityChanged
	protected final void _OnVisibilityChanged ()
	{
		int isAnyInstanceVisible = 0;
		
		for (WallpaperEngine eng : _instances) {
			isAnyInstanceVisible |= eng._visible ? 1 : 0;
		}
		
		if (isAnyInstanceVisible != 0)
		{
			for (WallpaperEngine eng : _instances) {
				if ( eng._visible ) {
					eng._SetSurface();
				}
			}
			engine.OnWindowFocusChanged( true );
		}
		else
		{
			engine.OnWindowFocusChanged( false );
			_Pause();
		}
	}
	
	
	// _Stop
	protected final void _Stop ()
	{
		if ( _state == STATE_PAUSED ) {
			Logger.I( TAG, "_Stop" );
			
			_state = STATE_STOPED;
			engine.OnStop();
		}
	}
	
	
	// _Pause
	protected final void _Pause ()
	{
		if ( _state == STATE_ACTIVE ) {
			Logger.I( TAG, "_Pause" );
			
			_state = STATE_PAUSED;
			engine.OnPause();
		}
	}

	
	
// types //
	
	//
	// Wallpaper Engine
	//
	protected final class WallpaperEngine extends Engine
	{
	// variables //
		private final int		_id;
		
		private SurfaceHolder	_surfaceHolder	= null;
		private int 			_surfaceFormat	= 0;
		private int 			_surfaceWidth	= 0;
		private int 			_surfaceHeight	= 0;
		
		private boolean			_visible		= false;
		
		
	// methods //
		
		// constructor
		public WallpaperEngine (int id)
		{
			_id = id;
		}
		
		
		// onCreate
		@Override
		public final void onCreate (SurfaceHolder surfaceHolder)
		{
			Logger.I( TAG, "WallpaperEngine " + _id + " OnCreate" );
			
			super.onCreate( surfaceHolder );
			
			_instances.add(this);
		}
		
		
		// onDestroy
		@Override
		public final void onDestroy ()
		{
			Logger.I( TAG, "WallpaperEngine " + _id + " onDestroy" );
			
			_instances.remove(this);
			
			super.onDestroy();
		}
		
		
		// onSurfaceCreated
		@Override
		public final void onSurfaceCreated (SurfaceHolder holder)
		{
			Logger.I( TAG, "WallpaperEngine " + _id + " onSurfaceCreated" );
			
			super.onSurfaceCreated( holder );
			_ResumeOnSurfaceCreated( holder );

			_surfaceHolder	= holder;
			_surfaceFormat	= 0;
			_surfaceWidth	= 0;
			_surfaceHeight	= 0;
		}

		
		// onSurfaceChanged
		@Override
		public final void onSurfaceChanged (SurfaceHolder holder, int format, int width, int height)
		{
			Logger.I( TAG, "WallpaperEngine " + _id + " onSurfaceChanged" );
			
			super.onSurfaceChanged( holder, format, width, height );
			_ResumeOnSurfaceChanged( holder, format, width, height );

			_surfaceHolder	= holder;
			_surfaceFormat	= format;
			_surfaceWidth	= width;
			_surfaceHeight	= height;
		}
		
		
		// onSurfaceDestroyed
		@Override
		public final void onSurfaceDestroyed (SurfaceHolder holder)
		{
			Logger.I( TAG, "WallpaperEngine " + _id + " onSurfaceDestroyed" );
			
			super.onSurfaceDestroyed( holder );
			engine.surfaceDestroyed( holder );

			_surfaceHolder	= null;
			_surfaceFormat	= 0;
			_surfaceWidth	= 0;
			_surfaceHeight	= 0;

			_OnVisibilityChanged();
		}
		
		
		// onTouchEvent
		@Override
		public final void onTouchEvent (MotionEvent event)
		{
			Logger.I( TAG, "WallpaperEngine " + _id + " onTouchEvent" );
			
			super.onTouchEvent( event );
			engine.onTouch( null, event );
		}
		
		
		// onVisibilityChanged
		@Override
		public final void onVisibilityChanged (boolean visible)
		{
			Logger.I( TAG, "WallpaperEngine " + _id + " onVisibilityChanged " + visible );
			
			_visible = visible;
			
			_OnVisibilityChanged();
		}
		
		
		// SetSurface
		private final void _SetSurface ()
		{
			Logger.I( TAG, "WallpaperEngine " + _id + " _SetSurface" );
			
			_ResumeOnSurfaceChanged( _surfaceHolder, _surfaceFormat, _surfaceWidth, _surfaceHeight );
		}
	}
}

// #endif WALLPAPER

